package com.java110.mall.goods.cmd.productCategory;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.product.ProductCategoryDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.shop.ShopDto;
import com.java110.intf.goods.IProductCategoryInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.goods.bmo.productCategory.IGetProductCategoryBMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "productCategory.queryAdminProductCategory")
public class QueryAdminProductCategoryCmd extends Cmd {

    @Autowired
    private IGetProductCategoryBMO getProductCategoryBMOImpl;

    @Autowired
    private IProductCategoryInnerServiceSMO productCategoryInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ProductCategoryDto productCategoryDto = BeanConvertUtil.covertBean(reqJson,ProductCategoryDto.class);
        int count = productCategoryInnerServiceSMOImpl.queryProductCategorysCount(productCategoryDto);

        List<ProductCategoryDto> productCategoryDtos = null;
        if (count > 0) {
            productCategoryDtos = productCategoryInnerServiceSMOImpl.queryProductCategorys(productCategoryDto);
            freshShopName(productCategoryDtos);
        } else {
            productCategoryDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) productCategoryDto.getRow()), count, productCategoryDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    private void freshShopName(List<ProductCategoryDto> productCategoryDtos) {
        if (ListUtil.isNull(productCategoryDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();

        for (ProductCategoryDto tmpProductCategoryDto : productCategoryDtos) {
            shopIds.add(tmpProductCategoryDto.getShopId());
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        for (ProductCategoryDto tmpProductCategoryDto : productCategoryDtos) {
            for (ShopDto tmpShopDto : shopDtos) {
                if (!tmpShopDto.getShopId().equals(tmpProductCategoryDto.getShopId())) {
                    continue;
                }
                tmpProductCategoryDto.setShopName(tmpShopDto.getShopName());
            }
        }
    }
}
